package org.wx.herostory.services.encoder;

import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
import org.wx.herostory.msg.GameMsgProtocol;
import org.wx.herostory.model.User;
import org.wx.herostory.services.util.Broadcaster;
import org.wx.herostory.services.util.UserManager;

/**
 * @program: my-java
 * @description: 用户入场
 * @author: Wang.xun
 * @create: 2021-03-01 21:43
 **/
@Slf4j
public class UserEntryCmdHandler implements ICmdHandler<GameMsgProtocol.UserEntryCmd>{
    @Override
    public void handle(ChannelHandlerContext ctx, GameMsgProtocol.UserEntryCmd cmd) {
        if(null == ctx || null == cmd){
            return;
        }

        // 用户ID
        int userId = (int) ctx.channel().attr(AttributeKey.valueOf("userId")).get();
        log.info("用户进场{}...",userId);
        // 英雄形象

        User user = UserManager.getUserById(userId);
        // 构建entryResult
        GameMsgProtocol.UserEntryResult.Builder builder = GameMsgProtocol.UserEntryResult.newBuilder();
        builder.setUserId(user.getUserId());
        builder.setUserName(user.getUserName());
        builder.setHeroAvatar(user.getHeroAvatar());
        GameMsgProtocol.UserEntryResult entryResult = builder.build();

        // 广播有英雄进场
        Broadcaster.broadcast(entryResult);
    }
}
